<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--{include file='comm/header.tpl'}-->
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/jscal2.css" />
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/border-radius.css" />
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/steel/steel.css" />
<script src="/framework/JSCal2/src/js/jscal2.js"></script>
<script src="/framework/JSCal2/src/js/lang/cn.js"></script>
<script src="/quanliadmin/docroot/js/commonCheck.js"></script>

    <!--{if $operFlg == 1}-->
    <title>商品套装添加</title>
    <!--{else}--> 
    <title>商品套装编辑</title>
    <!--{/if}-->
<style>
	html{
		overflow-y:scroll;
		overflow:-moz-scrollbars-vertical;
	}
</style>
</head>
<body>
	<div style="width:100%">
		
		<div class="items clearfix">
			<h2 style="" class="tal">
				<!--{if $operFlg == 1}-->
			    	商品套装添加
			    <!--{else}-->
			    	商品套装编辑
			    <!--{/if}-->
			</h2>
			
			<div class="items2">
				<!--{if $error_msg}-->
	            
				<div class="comm-box">
	                <div style="position:relative;color: red;">
	                    <!--{$error_msg}-->
	                </div>
	            </div>
				
	            <!--{else}-->
				<div style="margin:0 auto;">
						<table>
							<tr style="height: 35px;">
								<td width="10%" class="tar">套装名称：</td>
								<td width="" class="td_input">
									<input type="text" id="goods_group_name" name="goods_group_name" value="<!--{$groupInfo->goods_group_name}-->">
									<input type="hidden" id="goods_group_id" name="goods_group_id" value="<!--{$groupInfo->goods_group_id}-->">
									<input type="hidden" id="flg" name="flg" value="<!--{$flg}-->">
                                    <span class="red">*</span>
								</td>
							</tr>
                            <tr style="height: 35px;">
                                <td width="10%" class="tar">有效日期：</td>
                                <td width="*" class="td_input">
                                    <input type="text" id="startDate" name="startDate" style="width:100px;" readonly="readonly" value="<!--{$groupInfo->startDate}-->">&nbsp;
                                    ～
                                    <input type="text" id="endDate" name="endDate" style="width:100px;" readonly="readonly" value="<!--{$groupInfo->endDate}-->">&nbsp;
                                </td>
                            </tr>
                            <tr style="height: 35px;">
                                <td width="10%" class="tar">商品编号：</td>
                                <td width="*" class="td_input">
                                    <input type="text" id="main_product_code" name="main_product_code" value="<!--{$groupInfo->product_code}-->">
                                    &nbsp;
                                    <input type="button" id="cancelMain" value="取消" class="btn" <!--{if $operFlg == 1}--> style="display: none;" <!--{/if}--> onclick="package.cancelGoods();" />&nbsp;
                                    <span class="red">* (输入编号后请按回车键)</span>
                                </td>
                            </tr>
						</table>
				</div>
                <table class="s" id="package_goods" style="margin-top: 20px;<!--{if $operFlg == 1}-->display: none;<!--{/if}-->" >
                    <tr>
                        <th width="120px">商品编号</th>
                        <th width="*">商品名称</th>
                        <th width="110px">规格</th>
                        <th width="70px">销售价</th>
                        <th width="70px">套装单价</th>
                        <th width="50px">数量</th>
                        <th width="80px">赠送积分</th>
                        <th width="60px">操作</th>
                    </tr>

                    <!--{if $groupGoodsInfo }-->
                        <tr id='tr_<!--{$main_goods->product_code}-->' class='tr_goods' >
                            <td style='text-align: left;height:20px;'><!--{$main_goods->product_code}--></td>
                            <td><!--{$main_goods->goodsName}--></td>
                            <td><!--{$main_goods->spec}--></td>
                            <td><!--{$main_goods->salePrice}--></td>
                            <td id='discountPrice_<!--{$main_goods->product_code}-->'><!--{$main_goods->discountPrice}--></td>
                            <td id='goodsCount_<!--{$main_goods->product_code}-->'><!--{$main_goods->goodsCount}--></td>
                            <td id='salePoints_<!--{$main_goods->product_code}-->'><!--{$main_goods->salePoints}--></td>
                            <td style='text-align: center;height:20px;'></td>
                        </tr>
                        <!--{foreach from=$groupGoodsInfo item=goods}-->
                        <!--{if $goods->product_code != $groupInfo->product_code}-->
                        <tr id='tr_<!--{$goods->product_code}-->' class='tr_goods' >
                        <td style='text-align: left;height:20px;'><!--{$goods->product_code}--></td>
                        <td><!--{$goods->goodsName}--></td>
                        <td><!--{$goods->spec}--></td>
                        <td><!--{$goods->salePrice}--></td>
                        <td id='discountPrice_<!--{$goods->product_code}-->'><!--{$goods->discountPrice}--></td>
                        <td id='goodsCount_<!--{$goods->product_code}-->'><!--{$goods->goodsCount}--></td>
                        <td id='salePoints_<!--{$goods->product_code}-->'><!--{$goods->salePoints}--></td>
                        <td style='text-align: center;height:20px;'><a href='javascript:void(0);' onclick="package.del('<!--{$goods->product_code}-->');" >删除</a></td>
                        </tr>
                        <!--{/if}-->
                        <!--{/foreach}-->
                        <tr id='nextLine'>
                        <td  ><input id='product_code' style='width: 100px;height:20px;'  value=''  ></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td ><input id='discountPrice' style='width: 50px;height:20px;' value='' ></td>
                        <td ><input id='goodsCount' style='width: 50px;height:20px;' value='' ></td>
                        <td ><input id='salePoints' style='width: 50px;height:20px;' value='' ></td>
                        <td style='text-align: center;height:20px;'></td>
                        </tr>

                    <!--{/if}-->

                </table>
				<!--{/if}-->
			</div>
		</div>
		
		<div class="bottom_fixbox">
			<div class="rectbox tac">
				<input type="hidden" value="" id="dataCache" name="dataCache"/>
				<input type="button" class="btn" onclick="E.refresh();" value="重置">
	        	<!--{if $operFlg == 1}-->
	        		<input type="button" class="btn" onclick="package.save();" value="添加商品套装">
				<!--{else if $operFlg == 2}-->
					<input type="button" class="btn" onclick="package.save();" value="修改商品套装">
				<!--{/if}-->
				<input type="button" class="btn" onclick="E.closeWindows();" value="关闭">
	        </div>
		</div>
	</div>
	<script>

		//operFlg
		var operFlg = '<!--{$operFlg}-->';

        var goods_array = { };

		//页面初始化
		$(document).ready(function(){
			//新增页自动加载当月日期
			if (operFlg == 1){
				$("#startDate").val(currentFirstDay());
				$("#endDate").val(currentLastDay());
			}

			//选择商品编码输入框
			$('#product_code').select();
		});
		
		//空白编辑行商品编号输入框键盘事件
		$(document).on('keypress','#product_code',function(event){
			if (event.keyCode  == '13'){

				product_code = E.trim($("#product_code").val());
				if (product_code == '') {
					E.alert('请输入正确的商品编号');
					return false;
				}

                //检查是否已存在商品
               // var goods = $("#dataCache").data(product_code);
                var goods = goods_array[product_code];
                if(!E.empty(goods)){
                    E.alert("该商品已存在，请勿重复输入");
                    return;
                }

                package.addFlg = 0;    //标记添加行的来源
                package.checkGoods(product_code,package.addLine);
			}
		});
		
		//空白编辑行套装价输入框键盘事件
		$(document).on('keypress','#discountPrice',function(event){
			if (event.keyCode  == '13'){
				//阻止元素默认行为（提交表单）
				event.preventDefault();
				discountPrice = ($('#discountPrice').val() == '') ? 0 : E.trim($('#discountPrice').val());
				if (!E.isMoney(discountPrice) && discountPrice != 0 ){
					E.alert('请输入正确的价格格式');
					return false;
				}
				$('#goodsCount').select();
			}
		});
		
		//空白编辑行数量输入框键盘事件
		$(document).on('keypress','#goodsCount',function(event){
			if (event.keyCode  == '13'){
				//阻止元素默认行为（提交表单）
				event.preventDefault();
				goodsCount = E.trim($('#goodsCount').val());
				if (!E.isInt(goodsCount) || goodsCount == 0){
					E.alert('请输入正确的数量格式');
					return false;
				}
				$('#salePoints').select();
			}
		});
		
		//空白编辑行积分输入框键盘事件
		$(document).on('keypress','#salePoints',function(event){
			if (event.keyCode  == '13'){
                if(package.addFlg != 1){
                    var product_code = $.trim($('#product_code').val());
                    if (product_code == ''){
                        E.alert('请输入商品编号');
                        return false;
                    }
                }

                package.discountPrice = ($('#discountPrice').val() == '') ? 0 : E.trim($('#discountPrice').val());
				if (!E.isMoney(package.discountPrice) && package.discountPrice != 0 ){
					E.alert('请输入正确的价格格式');
					return false;
				}
                package.discountPrice = parseFloat(package.discountPrice).toFixed(2);

                package.goodsCount = E.trim($('#goodsCount').val());
				if (!E.isInt(package.goodsCount) || package.goodsCount == 0){
					E.alert('请输入正确的数量格式');
					return false;
				}
                package.salePoints = ($('#salePoints').val() == '') ? 0 : E.trim($('#salePoints').val());
				if (!E.isInt(package.salePoints)){
					E.alert('请输入正确的积分格式');
					return false;
				}

                package.produt_code = $("#salePoints").attr("produt_code");

                //去掉上一行的输入框,保存数据，显示下一个输入行，
				package.nextLine();
			}
		});

    $(document).ready(function (){
        $("#goods_group_name").focus();

        //套装名称回车
        $("#goods_group_name").keypress(function (e){
            if(e.keyCode == '13'){
                var goods_group_name = $.trim($(this).val());
                if(goods_group_name == ''){
                    E.alert("请输入套装名称");
                    return;
                }
                $("#main_product_code").focus();
            }
        });
        //商品编号回车事件
        $("#main_product_code").keypress(function (e){
            if(e.keyCode == 13){
                var product_code = $.trim($(this).val());
                if(product_code == ''){
                    E.alert("请输入商品编号");
                    return;
                }
                package.addFlg = 1; //标记该添加是从上部表单过去的
                package.checkGoods(product_code,package.addLine );
            }
        });
    });

    var package = {

        //商品编号的取消
        cancelGoods : function (){
            E.confirm("是否确认取消页面所有商品？","package.cancelGoodsConfirm");
        },

        cancelGoodsConfirm : function (){
            $(".tr_goods").remove();
            $("#nextLine").remove();
            $("#package_goods").css("display","none");
            $("#cancelMain").css("display","none");
            $("#main_product_code").val('');
            goods_array = { };
        },

        //根据商品编号获取商品信息
        checkGoods : function (product_code,func){

            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/promotion/package.ajax?operFlg=5",
                dataType: "JSON",
                data:{'product_code': product_code},
                success: function(response){
                    func(response);
                }
            });
        },

        //根据返回的商品数据，在表单中添加一行  package.addFflg :1 从上部表单的商品编号处添加
        addLine : function (response ){

            if(response.code != 200){
                E.alert(response.message);
                return;
            }

            if(package.addFlg == 1){
                $("#package_goods").css("display","inline-block");
                $("#cancelMain").css("display","inline-block");
            }

            var data = response.message

            $("#nextLine").remove();

            //添加页面HTML
            var html = "<tr id='tr_"+data.product_code+"' class='tr_goods' >";
            if(package.addFlg == 1){
                html += "<td style='text-align: left;height:20px;'>"+data.product_code+"</td>";
            }else{
                html += "<td id='product_code_"+data.product_code+"' ><input id='product_code' style='width: 100px;height:20px;'  value='"+data.product_code+"'  ></td>";
            }
            html += "<td>"+data.goodsName+"</td>";
            html += "<td>"+data.spec+"</td>";
            html += "<td>"+data.salePrice+"</td>";
            html += "<td id='discountPrice_"+data.product_code+"'><input id='discountPrice' style='width: 50px;height:20px;' value='"+data.discountPrice+"' ></td>";
            html += "<td id='goodsCount_"+data.product_code+"'><input id='goodsCount' style='width: 50px;height:20px;' value='"+data.goodsCount+"' ></td>";
            html += "<td id='salePoints_"+data.product_code+"'><input id='salePoints' style='width: 50px;height:20px;' value='"+data.salePoints+"' produt_code='"+data.produt_code+"' ></td>";
            if(package.addFlg == 1){
                html += "<td style='text-align: center;height:20px;'></td>";
            }else{
                html += "<td style='text-align: center;height:20px;'><a href='javascript:void(0);' id='cancel_"+data.product_code+"' class='hide'  onclick=\"package.del('"+data.product_code+"');\" >删除</a></td>";
            }
            html += "</tr>";

            $("#package_goods").append(html);

            //默认选中套装价
            $("#discountPrice").focus();

            package.goods_data = data;
        },

        //积分回车后，保存当前行数据至缓存,去掉当前行的输入框，显示下一个输入行
        nextLine : function (){

            //更新当前行数据至缓存
            package.goods_data.discountPrice = package.discountPrice;
            package.goods_data.goodsCount = package.goodsCount;
            package.goods_data.salePoints = package.salePoints;
          //  $("#dataCache").data(package.goods_data.product_code,package.goods_data);
            goods_array[package.goods_data.product_code] = package.goods_data;


            //去掉当前行的input
            $("#product_code_"+this.goods_data.product_code).html(this.goods_data.product_code);
            $("#discountPrice_"+this.goods_data.product_code).html(package.discountPrice);
            $("#goodsCount_"+this.goods_data.product_code).html(package.goodsCount);
            $("#salePoints_"+this.goods_data.product_code).html(package.salePoints);

            //显示当前行的删除
            $("#cancel_"+this.goods_data.product_code).removeClass("hide");


            var html = "<tr id='nextLine'>";
            html += "<td  ><input id='product_code' style='width: 100px;height:20px;'  value=''  ></td>";
            html += "<td></td>";
            html += "<td></td>";
            html += "<td></td>";
            html += "<td ><input id='discountPrice' style='width: 50px;height:20px;' value='' ></td>";
            html += "<td ><input id='goodsCount' style='width: 50px;height:20px;' value='' ></td>";
            html += "<td ><input id='salePoints' style='width: 50px;height:20px;' value='' ></td>";
            html += "<td style='text-align: center;height:20px;'></td>";
            html += "</tr>";

            $("#package_goods").append(html);

            $("#product_code").focus();

        },

        //删除当前行
        del : function (product_code){
            this.product_code = product_code;
            E.confirm("是否确认删除该商品？","package.delConfirm");
        },

        //删除确认
        delConfirm : function (){
            $("#tr_"+this.product_code).remove();
            //$("#dataCache").removeData(this.product_code);
            delete goods_array[this.product_code];
        },

        //保存
        save : function (){
            var error_msg = '';

            //处理输入内的数据
            var line_product_code = $.trim($("#product_code").val());
            var line_discountPrice = $.trim($("#discountPrice").val());
            var line_goodsCount = $.trim($("#goodsCount").val());
            var line_salePoints = $.trim($("#salePoints").val());
            //var obj = $("#dataCache").data(line_product_code);
            var obj = goods_array[line_product_code];

            if(line_product_code != '' && $.isEmptyObject(obj) ){

                if(!E.isMoney(line_discountPrice)){
                    error_msg += "请输入正确的套装单价<br/>";
                }
                if(!E.isInt(line_goodsCount) || line_goodsCount <= 0 ){
                    error_msg += "请输入正确的商品数量<br/>";
                }
                if(!E.isInt(line_salePoints)  ){
                    error_msg += "请输入正确的赠送积分<br/>";
                }
                package.goods_data.discountPrice = line_discountPrice;
                package.goods_data.goodsCount = line_goodsCount;
                package.goods_data.salePoints = line_salePoints;

              //  $("#dataCache").data(package.goods_data.produt_code,package.goods_data);
                goods_array[package.goods_data.product_code] = package.goods_data;
            }

            console.log(goods_array);

           // var groupGoodsInfo = $("#dataCache").data();
            var groupGoodsInfo = goods_array;

            var checkArr = new Array();

            $.each(groupGoodsInfo, function(k,v){
                checkArr.push(k);
            });

            var goods_group_id = $('#goods_group_id').val();
            var flg = $('#flg').val();
            var goods_group_name = $('#goods_group_name').val();
            var startDate = $('#startDate').val();
            var endDate = $('#endDate').val();

            if (goods_group_name == ''){
                error_msg += '套装名称不能为空<br/>';
            }
            if (startDate == ''){
                error_msg += '开始时间不能为空<br/>';
            }
            if (endDate == ''){
                error_msg += '结束时间不能为空<br/>';
            }
            if (startDate != '' && endDate != '' && startDate > endDate){
                error_msg += '开始时间不能大于结束时间<br/>';
            }


            if (endDate < currentDate()){
                error_msg += '有效期结束时间不能小于当前时间<br/>';
            }
            /************************必须有其他套装商品********************************/
            if (checkArr <= 1){
                error_msg += '请至少选择2件套装商品<br/>';
            }
            /************************必须有其他套装商品********************************/
            if (error_msg != ''){
                return E.alert(error_msg);
            }
            var product_code = $.trim($("#main_product_code").val());
            //主表数据
            var addForm = {
                goods_group_id : goods_group_id,
                flg : flg,
                goods_group_name : goods_group_name,
                startDate : startDate,
                endDate : endDate,
                product_code : product_code
            }
            $.ajax({
                type: "POST",
                <!--{if $operFlg == 1}-->
                url: "/invokingAjax-backend/promotion/package.ajax?operFlg=2",
                <!--{else}-->
                url: "/invokingAjax-backend/promotion/package.ajax?operFlg=3",
                <!--{/if}-->
                dataType: "JSON",
                data: {'mainInfo': addForm, 'groupGoodsInfo': groupGoodsInfo},
                success: function( result_obj ) {
                    if (result_obj.code == 200) {
                        E.opener("source.load");
                        if (operFlg == 1){
                            E.alert(result_obj.message,  2, "E.refresh");
                        }else{
                            E.alert(result_obj.message,  2, "E.closeWindows");
                        }
                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        }

    }

    <!--{if $groupGoodsInfo }-->
        <!--{foreach from=$groupGoodsInfo item=goods}-->
        var data = {
                product_code : "<!--{$goods->product_code}-->",
                goodsID : "<!--{$goods->goodsID}-->",
                postID : "<!--{$goods->postID}-->",
                goodsName : "<!--{$goods->goodsName}-->",
                salePrice : "<!--{$goods->salePrice}-->",
                discountPrice : "<!--{$goods->discountPrice}-->",
                salePoints : "<!--{$goods->salePoints}-->",
                goodsCount : "<!--{$goods->goodsCount}-->",
                link : "<!--{$goods->link}-->",
                img_link : "<!--{$goods->img_link}-->"
        }
       // $("#dataCache").data("<!--{$goods->product_code}-->",data);
           goods_array["<!--{$goods->product_code}-->"] = data;
        <!--{/foreach}-->

    <!--{/if}-->



	</script>
</body>
</html>
<script>
	//日期控件JS
    var cal = Calendar.setup({
        onSelect: function(cal) { cal.hide() }
    });
	cal.manageFields("startDate", "startDate", "%Y-%m-%d");
	cal.manageFields("endDate", "endDate", "%Y-%m-%d");
</script>